package com.konne.nightmare.FastPublicOpinion.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by ws.
 * on Data:2022/2/10  Time:下午 3:50.
 * description: 单选框组 .
 */
public class QJRadioGroup extends LinearLayout {
    private List<QJRadio> buttons=new ArrayList<>();
    private OnCheckedChangeListener onCheckedChangeListener;
    private int selIndex=-1;
    private QJRadio selRadioButton;
    public QJRadioGroup(Context context) {
        this(context,null);
    }

    public QJRadioGroup(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public QJRadioGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        for (int i = 0; i < getChildCount(); i++) {
            View view=getChildAt(i);
            if(view instanceof QJRadio){
                buttons.add((QJRadio) view);
            }else if(view instanceof ViewGroup){
                ViewGroup vp= (ViewGroup) view;
                for (int j = 0; j < vp.getChildCount(); j++) {
                    if(vp.getChildAt(j) instanceof QJRadio){
                        buttons.add((QJRadio) vp.getChildAt(j));
                    }
                }
            }
        }
        for (int i = 0; i < buttons.size(); i++) {
            if(buttons.get(i).isChecked()){
                selIndex=i;
                if(selRadioButton!=null){
                    selRadioButton.setCheck(false);
                }
                selRadioButton=buttons.get(i);
            }
            buttons.get(i).setOnCheckedChangeListener(itemChangeListener);
        }
        if(onCheckedChangeListener!=null){
            onCheckedChangeListener.onCheckedChanged(QJRadioGroup.this,selIndex);
        }
    }
    private QJRadio.OnCheckedChangeListener itemChangeListener=new QJRadio.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(QJRadio var1, boolean var2) {

            if(selRadioButton==var1 || var2==false){
                return;
            }
            if (selRadioButton!=null){
                selRadioButton.setCheck(false);
            }
            selRadioButton=var1;
            selIndex=buttons.indexOf(selRadioButton);
            if(onCheckedChangeListener!=null){
                onCheckedChangeListener.onCheckedChanged(QJRadioGroup.this,selIndex);
            }
        }
    };

    public List<QJRadio> getRadioButtons() {
        return buttons;
    }

    public interface OnCheckedChangeListener {
        void onCheckedChanged(QJRadioGroup var1, int var2);
    }
}
